﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BioBooking.Data;
using System.ComponentModel;

namespace BioBooking.Client.Screens
{
    public partial class EditMovies : UserControl
    {
        BioBooking.Data.IRepository service;
        ObservableCollection<Movie> activeMovies;
        ObservableCollection<Genre> allGenres;
        Movie selectedMovie;

        ICollectionView collectionView;

        public bool UnsavedChanges
        {
            get { return activeMovies.Any(p => p.EntityState == System.Data.EntityState.Modified); }
        }

        public EditMovies()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                service = Repository.GetRepository();

                activeMovies = new ObservableCollection<Movie>(service.ListAllActiveMovies());
                allGenres = new ObservableCollection<Genre>(service.GetAllGenres());

                filmListBox.DataContext = activeMovies;
                cbGenre.DataContext = allGenres;

                collectionView = CollectionViewSource.GetDefaultView(activeMovies);
                collectionView.Filter = new Predicate<object>(FilterCollection);
            }
        }

        public bool FilterCollection(object item)
        {
            Movie movie = item as Movie;

            if (movie == null)
                return false;

            return movie.Titel.IndexOf(txtSearch.Text, StringComparison.CurrentCultureIgnoreCase) >= 0;
        }

        private void searchBut_Click(object sender, RoutedEventArgs e)
        {
            collectionView.Refresh();
        }

        private void filmListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            #region oldstuff
            //int index = filmListBox.SelectedIndex;
            //if (index < 0)
            //    return;

            //Movie movie = activeMovies[index]; 
            #endregion

            Movie movie = filmListBox.SelectedItem as Movie;
            if (movie == null)
                return;

            selectedMovie = movie;

            btnDeleteMovie.IsEnabled = (movie.EntityState != System.Data.EntityState.Detached);
        }

        private void gemBut_Click(object sender, RoutedEventArgs e)
        {
            Movie movie = filmListBox.SelectedItem as Movie;

            if (movie == null)
                return;

            service.InsertMovieObject(movie);

            //string title = txtTitle.Text;
            //DateTime releaseDate = calYear.SelectedDate.Value;
            //string actors = txtActor.Text;
            //string originalTitel = txtOriginalTitle.Text;
            //string country = txtCountry.Text;
            //string language = txtLanguage.Text;
            //string description = txtDescription.Text;
            //Genre genre = allGenres[cbGenre.SelectedIndex];
            //string instructor = txtInstructor.Text;
            //service.insertMovie(title, calYear.SelectedDate.Value, instructor, actors, originalTitel, country, language, description, int.Parse(txtMovieLength.Text), genre);
        }

        private void delBut_Click(object sender, RoutedEventArgs e)
        {
            if (selectedMovie == null)
                return;

            if (MessageBox.Show(String.Format("Er du sikker på du vil slette filmen: {0}", selectedMovie.Titel), "Er du sikker?", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
            {
                return;
            }

            activeMovies.Remove(selectedMovie);

            if (filmListBox.Items.Count > 0)
            {
                filmListBox.SelectedIndex = 0;
            }

            if (selectedMovie.EntityState == System.Data.EntityState.Detached)
                return;

            service.DeleteMovie(selectedMovie);
        }

        private void soegeText_TextChanged(object sender, TextChangedEventArgs e)
        {
            collectionView.Refresh();
        }

        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            if (selectedMovie == null)
            {
                MessageBox.Show("Du skal først vælge en film");
                return;
            }

            service.UpdateMovieObject(selectedMovie);

            MessageBox.Show(String.Format("Film: {0} blev opdateret", selectedMovie.Titel), "Film Opdateret");
        }

        private void btnCreateMovie_Click(object sender, RoutedEventArgs e)
        {
            activeMovies.Add(new Movie() { Titel = "Unavngiven", ReleaseDate = DateTime.Now });
            collectionView.Refresh();
            filmListBox.SelectedIndex = (filmListBox.Items.Count - 1);
        }
    }
}
